package weekMatch.one_349;

public class LeeCode_03 {
    public static void main(String[] args) {
        System.out.println(minCost(new int[]{31,25,18,59,31,25,18}, 27));
    }
    private static long minCost(int[] nums, int x) {
        int n = nums.length;
        long ans = Long.MAX_VALUE;
        int[] grid = new int[n * 2];
        for (int i = 0; i < grid.length; i++) {
            grid[i] = nums[i % n];
        }
        int[][] tab = new int[n][n];
        for (int i = 0; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < n; j++) {
                min = Math.min(grid[i + j], min);
                tab[i][j] = min;
            }
        }
        for (int i = 0; i < n; i++) {
            long sum = 0;
            sum += (long) i * x;
            for (int j = 0; j < n; j++) {
                sum += tab[j][i];
            }
            ans = Math.min(sum, ans);
        }
        return ans;
    }
}